import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from "@/stores/user"
import Layout from '@/Layout/index.vue'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      component: Layout,
      children: [{
        path: '',
        name: 'Home',
        component: () => import('@/views/home/index.vue'),
        meta: {
          current: 'Home'  //头部标签选中
        }
      }, {
        path: '/course',
        name: 'Course',
        component: () => import('@/views/course/index.vue'),
        meta: {
          current: 'Course'
        }
      }, {
        path: `/detail/:id`,
        name: 'Detail',
        component: () => import('@/views/detail/index.vue'),
        meta: {
          current: 'Course'
        }
      }, {
        path: '/course-play/:courseId/:chapterId',
        name: 'course-play',
        component: () =>
          import("@/views/CoursePlay/index.vue"),
      }, {
        path: '/cart',
        name: 'cart',
        component: () => import("@/views/cart/index.vue"),
        beforeEnter: (to, from, next) => {
          if (useUserStore().userInfo.id) {
            next()
          } else {
            next('/login')
          }
        }
      }, {
        path: '/confirmOrder',
        name: 'confirmOrder',
        component: () => import("@/views/confirmOrder/index.vue"),
        beforeEnter: (to, from, next) => {
          if (useUserStore().userInfo.id) {
            next()
          } else {
            next('/login')
          }
        }
      }, {
        path: '/about',
        name: 'About',
        component: () => import('@/views/about/index.vue'),
        meta: {
          current: 'About'
        }
      }, {
        path: '/login',
        name: 'Login',
        component: () => import('@/views/login/index.vue'),
        meta: {
          current: 'login'
        }
      },]
    },

  ]
})

export default router
